<template>
  <div>
    <!-- 入院管理 -->
    <!-- 搜索 -->
    <el-card class="card">
      <el-form ref="ruleFormRef" :inline="true" class="demo-form-inline" :model="params">
        <el-form-item label="老人姓名：" prop="name">
          <el-input v-model="params.name" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="身份证号：" prop="idCard">
          <el-input v-model="params.idCard" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="床位：" prop="begId">
          <el-cascader
            v-model="params.begId"
            :props="props"
            :options="getcounts"
            clearable
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="状态：" prop="state">
          <el-select v-model="params.state!" clearable style="width: 200px">
            <el-option v-for="item in stateList" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="search">查询</el-button>
          <el-button :icon="Refresh" @click="resetForm(ruleFormRef)">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格 -->
    <el-card class="card">
      <ELTable v-bind="tableConfig" ref="hosTable">
        <template #elderlyGender="{ scope }">
          {{ scope.row.elderlyGender == "1" ? "男" : "女" }}
        </template>
        <template #addTime="{ scope }">
          {{ dayJs(scope.row.addTime).format("YYYY-MM-DD HH:mm") }}
        </template>
        <template #state="{ scope }">
          {{ scope.row.state == 1 ? "已入院" : "未入院" }}
        </template>
        <template #operate="{ scope, deleteHandle }">
          <el-button type="primary" :icon="Edit" text @click="update(scope.row)">编辑</el-button>
          <el-button type="danger" :icon="Delete" text @click="deleteHandle(scope.row.id)">
            删除
          </el-button>
          <el-button type="primary" :icon="Document" text @click="toDetail(scope.row)">
            详情
          </el-button>
        </template>

        <template #btns="{ deleteAll }">
          <el-button type="success" :icon="Plus" @click="add">新增</el-button>
          <el-button :icon="Delete" type="danger" @click="deleteAll">批量删除</el-button>
        </template>
      </ELTable>
    </el-card>
    <selectElderly v-if="dialogVisible" @close="closeHandle" />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import ELTable, { TableData } from "@/components/ELTable.vue";
import selectElderly from "../../components/market/hospitalized/elderly/selectElderly.vue";
import { delById, getHospitalizedList, delMany } from "../../api/market/hospitalized/hospitalized";
import { ListParams } from "../../api/market/hospitalized/type";
import { Document, Delete, Edit, Search, Refresh, Plus } from "@element-plus/icons-vue";
import dayJs from "dayjs";
import router from "@/router";
import { bedList } from "@/api/config/bed/bed";
import { buildTree } from "@/utils/treeUtil";
import { badListParams } from "@/api/config/bed/type";
import { houseList } from "@/api/config/house/house";
import { listParams } from "@/api/config/house/type";
import { buildingListData } from "@/api/config/building/type";
import { buildingList } from "@/api/config/building/building";

const hosTable = ref();

//级联选择器格式转换
const props = { label: "name", value: "id" };

// 楼栋列表
const buildList = ref<buildingListData[]>([]);
const getBuildingList = async () => {
  let res = await buildingList();
  buildList.value = res.list;
  getHouseList();
};

//房间列表
const houseParams = reactive<listParams>({
  page: 1,
  pageSize: 1000,
  name: "",
  buildingId: undefined,
});
const house_list = ref<any[]>([]);
const getHouseList = async () => {
  let res = await houseList(houseParams);
  house_list.value = res.list.map((item) => ({
    id: item.id,
    name: item.name,
    pid: item.buildingId,
  }));
  getBedList();
};
//床位
const bedParams = reactive<badListParams>({
  houseId: undefined,
  page: 1,
  pageSize: 1000,
});
const bedListData = ref<any>([]);
const getBedList = async () => {
  let res = await bedList(bedParams);
  bedListData.value = res.list.map((item) => ({
    id: item.id,
    name: item.name,
    pid: item.houseId,
  }));
  mergeData([...buildList.value, ...house_list.value, ...bedListData.value]);
  // console.log("1111",bedListData.value);
};

//选择床位
const handleChange = (data: any) => {
  // console.log("7777data",data);

  if (data) {
    bedParams.houseId = data[data.length - 1];
    params.begId = data[data.length - 1];
    getBedList();
  }
};

// 合并
const getcounts = ref([]);
const mergeData = (data: any) => {
  getcounts.value = buildTree(data);
  // console.log("getCounts999",getcounts.value);
};

const ruleFormRef = ref<FormInstance>();

//查询参数
const params = reactive<ListParams>({});

//新增dialog
let dialogVisible = ref(false);

//状态
const stateList = ref([
  { id: 1, name: "未入院" },
  { id: 2, name: "已入院" },
]);

//查询
const search = () => {
  hosTable.value.getData(params);
};
//重置
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const tableConfig = reactive<TableData>({
  loadData: getHospitalizedList,
  delete: delById,
  deleteAll: delMany,
  columns: [
    {
      prop: "id",
      label: "序号",
      attrs: {
        width: 50,
      },
    },
    {
      prop: "elderlyPhoto",
      label: "头像",
      image: true,
      attrs: {
        width: 80,
      },
    },
    {
      prop: "elderlyGender",
      label: "性别",
      slot: true,
      attrs: {
        width: 50,
      },
    },
    {
      prop: "elderlyName",
      label: "老人姓名",
    },
    {
      prop: "elderlyIdCard",
      label: "身份证号",
    },
    {
      prop: "begName",
      label: "床位",
    },
    {
      prop: "addTime",
      label: "申请时间",
      slot: true,
    },
    {
      prop: "startDate",
      label: "入住开始时间",
    },
    {
      prop: "payDays",
      label: "入住时长（月）",
    },
    {
      prop: "state",
      label: "状态",
      slot: true,
    },
    {
      prop: "operate",
      label: "操作",
      slot: true,
      attrs: {
        width: 300,
      },
    },
  ],
});
//添加
const add = () => {
  dialogVisible.value = true;
};
//修改
const update = (data: any) => {
  //跳转修改
  router.push(`/market/order?oldId=${data.elderlyId}&ids=${data.id}`);
};

//关闭dialog
const closeHandle = () => {
  dialogVisible.value = false;
};

//跳转详情
const toDetail = (data: any) => {
  router.push(`/market/hospital-details/${data.id}?elderlyId=${data.elderlyId}`);
};

onMounted(() => {
  getBuildingList();
});
</script>

<style scoped>
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>
